<?php

namespace Index\Controller;

class UserController extends CommonController {
	public function userlist() {
// 		$data = M ( 'user' )->select ();
		
// 		$cnt = count ( $data );
// 		for($i = 0; $i < $cnt; $i ++) {
// 			$where['id'] = $data[$i]['group'];
// 			$sql = M('user_group')->where($where)->select();
// 			$data [$i] ['sex'] = $data [$i] ['sex'] ? '男' : '女'; // 1:男，0:女
// 			switch ($data [$i] ['group']) {
// 				case 1 :
// 					$data [$i] ['group'] = '管理员';
// 					break;
// 				case 2 :
// 					$data [$i] ['group'] = '一般用户';
// 					break;
// 				default :
// 					$data [$i] ['group'] = '未知';
// 			}
// 		}

// 		$this->group = $sql;
// 		$this->users = $data;

		$model = new \Think\Model();
		$sql = "CALL show_user_all()";	//执行存储过程查询操作
		$userlist = $model->query($sql);
		
		///$User = M('User'); // 实例化User对象
		// 进行分页数据查询 注意page方法的参数的前面部分是当前的页数使用 $_GET[p]获取
		//$list = $sql->where('status=1')->order('create_time')->page($_GET['p'].',25')->select();
		$this->assign('list',$userlist);// 赋值数据集
		$count      = count($userlist);// 查询满足要求的总记录数
		$Page       = new \Think\Page($count,2);// 实例化分页类 传入总记录数和每页显示的记录数
		$Page->setConfig('header','条记录');
		$Page->setConfig('prev','上一页');
		$Page->setConfig('next','下一页');
		$Page->setConfig('first','第一页');
		$Page->setConfig('last',"尾页");
		
		$show       = $Page->show();// 分页显示输出
		$this->assign('page',$show);// 赋值分页输出
		//$this->display(); // 输出模板
		
		$this->users = $userlist;
		
		$this->display ();
	}
	
	// 查看用户信息
	public function viewUser() {
		$id = I ( 'get.id', - 1, 'intval' );
		if ($id == - 1) {
			$this->error ( '查看用户信息失败！' );
		}
		
		$model = new \Think\Model();
		$sql = "CALL show_user_one(1, " . $id . ")";	//执行存储过程查询操作
		$viewUser = $model->query($sql);
		
		$data = $viewUser[0];
		$this->viewUser = $data;
		
		$this->display ();
	}
	
	// 处理删除用户
	public function delUser() {
		if (! IS_GET) {
			$this->error ( '页面不存在！' );
		}
		if($this->isAdmin()){
			$id = I ( 'get.id', - 1, 'intval' );
			if ($id == - 1) {
				$this->error ( '修改用户信息失败！' );
			}
			
			$user = M('user');
			if ($user->where(array('id'=>$id))->delete()) {
				//$this->success ( '删除成功', U ( 'userlist' ) );
				$this->redirect('/User/userlist');
			} else {
				$this->error ( '删除失败', U ( 'userlist' ) );
			}
		}else{
			$this->error('您没有权限进行操作！');
		}
	}
	
	// 修改密码视图
	public function editPass() {
		$this->display ();
	}
	// 处理修改密码
	public function doEditPass() {
		if (! IS_POST) {
			$this->error ( '页面不存在！' );
		}
		$opassword = md5(I('opassword'));
		$password = md5(I('password'));
		$id = $_SESSION['uid'];
		
		$user = M('user');
		$where = array(
			'id' => $id,
			'password' => $opassword
		);
		
		$check = $user->where($where)->limit(1)->find();
		
		if(!$check){
			$this->error('修改失败，原密码错误！');
		}
		$where1 = array(
			'id' => $id
		);
		$res = $user->where($where1)->save(array('password'=>$password));
		if($res){
			$this->success('密码修改成功！',U('userlist'));
		}else{
			$this->error('修改失败！',U('editPass'));
		}
		
	}
	
	// 
	public function user() {
		$this->display ();
	}
	
	// 用户添加视图
	public function addUser() {
		$this->group = M('user_group')->select();
		$this->display ();
	}
	
	// 处理添加用户
	public function doAddUser() {
		if (! IS_POST) {
			$this->error ( '页面不存在！' );
		} else {
			if($this->isAdmin()){
				$value = $_POST['username'];
				$data = array(
					'username'		=>	$value,
					'password'		=>	md5($_POST['password']),
					'sex'			=>	$_POST['sex'],
					'mail'			=>	$_POST['mail'],
					'group'			=>	$_POST['group'],
					'regtime'		=>	date('Y-m-d H:i:s'),
					'guid'			=>	$this->create_guid()
				);
				if($this->isSame('username', $value, 'user')){
					if (M ( 'user' )->add ( $data )) {
						$this->success ( '添加成功', U ( 'userlist' ) );
					} else {
						$this->error ( '添加失败', U ( 'addUser' ) );
					}
				}else{
					$this->error('添加失败，已经有此用户！',U('addUser'));
				}
			}else{
				$this->error('您没有权限执行此操作！');
			}
			
		}
		// $this->display();
	}
	
	// 编辑用户视图
	public function editUser() {
		$id = I ( 'get.id', - 1, 'intval' );
		if ($id == - 1) {
			$this->error ( '修改用户信息失败！' );
		}
		
		$sql = M ( 'User' );
		$data ['id'] = $id;
		
		$editUser = $sql->where ( $data )->find ();
		
		$group = M('user_group') ->select();
		
		$this->assign('group',$group);
		
		$this->editUser = $editUser;
		
		$this->display ();
	}
	// 处理编辑用户
	public function doEditUser() {
		if (! IS_POST) {
			$this->error ( '页面不存在！' );
		} 
		if($this->isAdmin()){
			if($this->isSame('username', $_POST['username'], 'user')){
				// redirect(__APP__);
				$id = I('post.id', -1, 'intval');
				if ($id == -1) {
					$this->error('修改用户信息失败！');
				}
				 //var_dump($_POST);
				if (M ( 'user' )->where('id='.$id)->save ( $_POST )) {
					$this->success ( '修改成功', U ( 'userlist' ) );
				} else {
					$this->error ( '修改失败', U ( 'userlist' ) );
				}
			}else{
				$this->error('修改失败，已有相同用户名！');		
			}
			
		}else{
			$this->error('您没有权限进行操作！');
		}
	}
}